package cn.hftoday.org;

import cn.hftoday.db.*;
import junit.framework.TestCase;
import cn.hftoday.util.*;
import cn.hftoday.core.*;

public class PersonTest extends TestCase {

	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}

	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testGetId() {
		Person p = new Person();
		p.setId("abcdefghi889");
		assertEquals(p.getId(), "abcdefghi889");
	}

	public void testGetName() {
		Person p = new Person();
		p.setName("bush");
		assertEquals(p.getName(), "bush");
	}
	
	public void testGetIcon() {
		Person p = new Person();
		assertEquals(p.getIcon(), Person.ICON);
	}
	
	public void testIsHidden() {
		Person p = new Person();
		p.setHidden(true);
		assertEquals(p.isHidden(), true);
	}

	public void testGetCreateTime() {
		Person p = new Person();
		p.setCreateTime(CalendarParser.toCalendar("2008-12-30 15:00:00"));
		assertEquals(p.getCreateTime(), CalendarParser.toCalendar("2008-12-30 15:00:00"));
	}

	public void testGetUpdateTime() {
		Person p = new Person();
		p.setUpdateTime(CalendarParser.toCalendar("2008-12-30 15:00:00"));
		assertEquals(p.getUpdateTime(), CalendarParser.toCalendar("2008-12-30 15:00:00"));
	}
	
	public void testGetCode() {
		Person p = new Person();
		p.setCode("bush");
		assertEquals(p.getCode(), "bush");
	}
	
	public void testGetBirthday() {
		Person p = new Person();
		p.setBirthday(CalendarParser.toCalendar("2008-12-30 15:00:00"));
		assertEquals(p.getBirthday(), CalendarParser.toCalendar("2008-12-30 15:00:00"));
	}
	
	public void testGetEnterTime() {
		Person p = new Person();
		p.setEnterTime(CalendarParser.toCalendar("2008-12-30 15:00:00"));
		assertEquals(p.getEnterTime(), CalendarParser.toCalendar("2008-12-30 15:00:00"));
	}
	
	public void testGetIdCard() {
		Person p = new Person();
		p.setIdCard("bush");
		assertEquals(p.getIdCard(), "bush");
	}
	
	public void testGetTitle() {
		Person p = new Person();
		p.setTitle("bush");
		assertEquals(p.getTitle(), "bush");
	}
	
	public void testGetPrivatePhone() {
		Person p = new Person();
		p.setPrivatePhone("bush");
		assertEquals(p.getPrivatePhone(), "bush");
	}
	
	public void testGetWorkPhone() {
		Person p = new Person();
		p.setWorkPhone("bush");
		assertEquals(p.getWorkPhone(), "bush");
	}
	
	public void testGetAddress() {
		Person p = new Person();
		p.setAddress("bush");
		assertEquals(p.getAddress(), "bush");
	}
	
	public void testGetAccount() {
		//insert account instance for test
		Person p = new Person();
		p.setId("1234567890009");
		
		Account acct = new Account();
		acct.setId("0987654332245");
		acct.setName("xiyangyang");
		acct.setEmail("xiyy@hftoday.cn");
		acct.setValid(true);
		acct.setPerson(p);
		Mapper.getInstance().insert("InsertAccount", acct);
		
		assertEquals(p.getAccount().getId(), "0987654332245");
	}
	
	public void testGetCompany() {
		//insert a company instance for test
		Company c = new Company();
		c.setId("c764faf7-8270-46a2-b78c-62959ee48d95");
		c.setName("abcde");
		Mapper.getInstance().insert("InsertCompany", c);
		
		//test person
		Person p = new Person();
		p.setCompany(c);
		
		Company c2 = p.getCompany();
		assertEquals(c2.getId(), "c764faf7-8270-46a2-b78c-62959ee48d95"); 
		assertEquals(c2.getName(), "abcde"); 
	}
	
	public void testSelect() {
		testInsert();
		
		Person p = (Person)Mapper.getInstance().queryForObject("SelectPersonById", "23456788900opq");
		assertEquals(p.getName(), "huitailang");
		assertEquals(p.getCreateTime(), CalendarParser.toCalendar("2009-01-14 00:00:00"));
		assertEquals(p.getUpdateTime(), CalendarParser.toCalendar("2009-01-15 00:00:00"));
		assertEquals(p.getCode(), "050001");
		assertEquals(p.getBirthday(), CalendarParser.toCalendar("2006-10-25 00:00:00"));
		assertEquals(p.getEnterTime(), CalendarParser.toCalendar("2007-07-15 00:00:00"));
		assertEquals(p.getIdCard(), "03040-50607(8)");
		assertEquals(p.getTitle(), "mr");
		assertEquals(p.getPrivatePhone(), "911''1");
		assertEquals(p.getWorkPhone(), "119\\-2");
		assertEquals(p.getAddress(), "050001");
	}

	public void testInsert() {
		Person p = new Person();
		p.setId("23456788900opq");
		p.setName("huitailang");
		p.setHidden(false);
		p.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:00"));
		p.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:00"));
		p.setCode("050001");
		p.setBirthday(CalendarParser.toCalendar("2006-10-25 00:00:00"));
		p.setEnterTime(CalendarParser.toCalendar("2007-07-15 00:00:00"));
		p.setIdCard("03040-50607(8)");
		p.setTitle("mr");
		p.setPrivatePhone("911''1");
		p.setWorkPhone("119\\-2");
		p.setAddress("050001");
		Object o = Mapper.getInstance().insert("InsertPerson", p);
		assertSame(p, o);
	}
	
	public void testUpdate() {
		testInsert();
		Person p = (Person)Mapper.getInstance().queryForObject("SelectPersonById", "23456788900opq");
		p.setName("huitailang2");
		p.setHidden(true);
		p.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:03"));
		p.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:04"));
		p.setCode("050008");
		p.setBirthday(CalendarParser.toCalendar("2006-10-25 00:00:05"));
		p.setEnterTime(CalendarParser.toCalendar("2007-07-15 00:00:06"));
		p.setIdCard("03040-50607(10)");
		p.setTitle("mrs");
		p.setPrivatePhone("911''2");
		p.setWorkPhone("119\\-7");
		p.setAddress("05000100");
		Mapper.getInstance().update("UpdatePerson", p);
		
		p = (Person)Mapper.getInstance().queryForObject("SelectPersonById", "23456788900opq");
		p.setHidden(true);
		assertEquals(p.getName(), "huitailang2");
		assertEquals(p.getCreateTime(), CalendarParser.toCalendar("2009-01-14 00:00:03"));
		assertEquals(p.getUpdateTime(), CalendarParser.toCalendar("2009-01-15 00:00:04"));
		assertEquals(p.getCode(), "050008");
		assertEquals(p.getBirthday(), CalendarParser.toCalendar("2006-10-25 00:00:05"));
		assertEquals(p.getEnterTime(), CalendarParser.toCalendar("2007-07-15 00:00:06"));
		assertEquals(p.getIdCard(), "03040-50607(10)");
		assertEquals(p.getTitle(), "mrs");
		assertEquals(p.getPrivatePhone(), "911''2");
		assertEquals(p.getWorkPhone(), "119\\-7");
		assertEquals(p.getAddress(), "05000100");
	}
	
	public void testDelete() {
		testInsert();
		Person p = (Person)Mapper.getInstance().queryForObject("SelectPersonById", "23456788900opq");
		Mapper.getInstance().delete("DeletePerson", p);
		p = (Person)Mapper.getInstance().queryForObject("SelectPersonById", "23456788900opq");
		assertEquals(p, null);
	}
}
